package com.purchase.ml.mapper.dealer;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.purchase.ml.bean.req.BaseDto;
import com.purchase.ml.bean.req.dealer.SysDealerNewDto;
import com.purchase.ml.entity.dealer.SysDealer;
import com.purchase.ml.entity.dealer.SysDealerPruduct;
import com.purchase.ml.entity.dealer.SysResion;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface SysDealerMapper extends BaseMapper<SysDealer> {

    //更新全部子商户的needpay
    @Update("update sys_dealer set need_pay = #{needPay} where superior=#{dealerId}")
    Long updateSonDealerNeedPay(@Param("dealerId") Long dealerId,@Param("needPay") Integer needPay);

    @Select("select dealer_id as dealerId from sys_dealer where dealer_name = #{dealerName}")
    Long selectDealerIdByName(@Param("dealerName") String dealerName);

    @Select("select * from sys_dealer where dealer_id = #{dealerId}")
    SysDealer selectByDealerId(@Param("dealerId") Long dealerId);

//    选取有且只有一个 持有者
    @Select("select * from sys_dealer where dealer_type = #{dealerType} limit 1")
    SysDealer selectAgencyDate(@Param("dealerType") int dealerType);

    /**
     * 通过商户id和商品id查询该商户商品信息
     * @param dealerId
     * @param productId
     * @return
     */
    @Select("select * from sys_dealer_pruduct where dealer_id=#{dealerId} and product_id=#{productId}")
    SysDealerPruduct findMerchantStock(@Param("dealerId") Long dealerId, @Param("productId")Long productId);

    /**
     * 删除经销商==》修改经销商状态
     * @param id
     * @return
     */
    @Update("update sys_dealer set state=#{state} where id=#{id}")
    int updateDealerState(@Param("id") Long id,@Param("state") Integer state);

    /**
     * 更新为已支付
    * */
    @Update("update sys_dealer set is_paid=1 where dealer_id=#{dealerId}")
    int updateDealerIsPaid(@Param("dealerId") Long dealerId);

    @Insert("insert into sys_reason(`register_resion`,`dealer_id`) values(#{sysResion.registerResion},#{sysResion.dealerId} )")
    int insertDealerResion(@Param("sysResion") SysResion sysResion);

    @Select("select * from sys_reason where dealer_id=#{dealerId} ")
    SysResion selectRegisterResion(@Param("dealerId") Long dealerId);

    @Update("update sys_dealer set state=#{state} where dealer_id=#{dealerId}")
    int updateDealerStateByDealerId(@Param("dealerId") Long dealerId,@Param("state") Integer state);
}
